package com.starsoft.verifycode;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtil {

	public static File getResourceFile(String path){
		String absolutPath = FileUtil.getResourcePath(path);
		return new File(absolutPath);
	}
	
	public static String getResourcePath(String path){
		if (!path.startsWith("/") && !path.startsWith("\\")) {
			path = "/" + path;
		}
		String absolutPath = FileUtil.class.getResource(path).getFile();
		return absolutPath;
	}

    public static void moveFile(File sourceFile, File targetFile){
    	sourceFile.renameTo(targetFile);
    }
    
    public static void copyFile(File sourceFile, File targetFile) throws IOException {
    	if(!targetFile.exists())
    		targetFile.createNewFile();
        BufferedInputStream inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
        BufferedOutputStream outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
        byte[] b = new byte[1024 * 5];
        int len;
        while ((len = inBuff.read(b)) != -1) {
            outBuff.write(b, 0, len);
        }
        outBuff.flush();
        outBuff.close();
        inBuff.close();
    }
}